package me.huha.utils;

import java.io.IOException;
import java.util.Set;

import net.sf.json.JSONObject;

import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

/**
 * @Title: JsonUtils.java
 * @Author: Yepx
 * @mail: yepx2013@gmail.com
 * @CreateTime: 2017年9月29日 下午3:24:04
 * @Todo: TODO
 */
public class JsonUtils {
	
	// Java实体对象转json对象
    public static XContentBuilder model2Json(JSONObject jsonObject) {
    	XContentBuilder jsonBuild = null;
        try {

        	jsonBuild = XContentFactory.jsonBuilder();
        	XContentBuilder xContentBuilder = jsonBuild.startObject();
        	
        	Set<?> keySet = jsonObject.keySet();
        	for (Object object : keySet) {
        		String key = object.toString();
        		xContentBuilder.field(key, jsonObject.get(key));
			}
        	
        	
        	xContentBuilder.endObject();
            
        } catch (IOException e) {
            e.printStackTrace();
        }

        return jsonBuild;
    }
}
